#include<bits/stdc++.h>
using namespace std;
const int N=131080;
int n,m,T,a[N],ta[N],dep[N],xx[4];
pair<int,int> c[N];
#define x first
#define id second
string s;
bool vs[21][N];
vector<int> cand,cand2;
int main(){
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cin>>n>>m;
    for(int i=1;i<=n;i++) cin>>ta[i];
    for(int i=1;i<=m;i++)
        cin>>c[i].x,c[i].id=i;
    sort(c+1,c+m+1);
    int k=1,D=0;
    while(k<n){
        for(int i=k+1;i<=(k<<1);i++) dep[i]=D+1;
        D++;k<<=1;
    }
    k>>=1;
    for(int i=D;i>=1;i--){
        cin>>s;
        for(int j=0;j<k;j++)
            vs[i][j+1]=s[j]-'0';
    }
    cin>>T;
    while(T--){
        for(int i=0;i<4;i++) cin>>xx[i];
        for(int i=1;i<=n;i++)
            a[i]=ta[i]^xx[i%4];
        long long ans=0;
        for(int j=1;j<=m;j++){
            if(c[j].x==1){
                ans^=c[j].id;
                continue;
            }
            cand.clear();
            for(int p=1;p<=(1<<dep[c[j].x]);p++)
                cand.push_back(p);
            for(int d=dep[c[j].x];d>=1;d--){
                int r=dep[c[j].x]-d+1;
                cand2.clear();
                for(int z=0;z<cand.size();z+=2){
                    if(vs[d][z/2+1]&&cand[z+1]<=c[j].x)
                        cand2.push_back((a[cand[z+1]]>=r?cand[z+1]:cand[z]));
                    else if(!vs[d][z/2+1]&&cand[z]<=c[j].x)
                        cand2.push_back((a[cand[z]]>=r?cand[z]:cand[z+1]));
                    else
                        cand2.push_back(cand[z]+cand[z+1]);
                }
                cand=cand2;
            }
            ans^=(1ll*cand[0]*c[j].id);
        }
        cout<<ans<<'\n';
    }
    return 0;
}
